package com.ensibs.nuitinfo2013;

import java.util.LinkedList;
import java.util.List;

import com.ensibs.nuitinfo2013.model.Collection;
import com.ensibs.nuitinfo2013.model.CollectionPersonnelle;
import com.ensibs.nuitinfo2013.model.Topic;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class CreationActivity extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.creation);
	}
	
	
	
	public void onClick(final View view) {
		switch (view.getId()) {
			case R.id.creation_button_envoyer:
				String titre = ((EditText) findViewById(R.id.creation_edit_titre)).getText().toString();
				String question = ((EditText) findViewById(R.id.creation_edit_question)).getText().toString();
				String auteur = ((EditText) findViewById(R.id.creation_edit_auteur)).getText().toString();
				String tag = ((Spinner) findViewById(R.id.creation_edit_tags)).getSelectedItem().toString();
				
				if (titre == null || titre.equals("") ||
						question == null || question.equals("") ||
								auteur == null || auteur.equals("") ||
								tag == null || tag.equals(""))
					alert();
				else {
					Topic topic = new Topic();
					topic.setTitre(titre);
					topic.setQuestion(question);
					topic.setAuteur(auteur);
					topic.setResolu(false);
					List<String> list = new LinkedList<String>();
					list.add(tag);
					topic.setTags(list);
					
					//if (Collection.getInstance().add(topic)) {
					if (Collection.getInstance().createTopic(topic.getTitre(), topic.getQuestion())) {
						if (CollectionPersonnelle.getInstance().add(Collection.getTopicByTitle(topic.getTitre()).getId())) {
							Toast.makeText(CreationActivity.this, getString(R.string.creation_toast), Toast.LENGTH_SHORT).show();
							finish();
						}
					}
				}
				break;
		}
	}
	
	private void alert() {
		AlertDialog.Builder alert = new AlertDialog.Builder(CreationActivity.this);
		alert.setTitle(getString(R.string.creation_alert_titre));
    	// set dialog message
		alert.setMessage(getString(R.string.creation_alert_contenu));
		
		alert.setPositiveButton(getString(R.string.creation_alert_bouton), new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		AlertDialog alertDialogHelp = alert.create();
    	alertDialogHelp.show();
	}
}
